package com.ym.utils;
import java.util.Collection;
import java.util.regex.Pattern;

/**
 * 方法描述:工具类
 * 继承xutils.Validators 扩展验证方法
 * <p>
 * author LZH
 * version v1.0
 * date 2016/6/16 09:51
 */
public class XValidators{

    public XValidators() {
    }

    public static boolean isAlphanumeric(String str) {
        return isRegexMatch(str, "[a-zA-Z0-9]+");
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isChinaMobile(String str) {
        return isRegexMatch(str, "1(3[4-9]|4[7]|5[012789]|8[278])\\d{8}");
    }

    public static boolean isChinaUnicom(String str) {
        return isRegexMatch(str, "1(3[0-2]|5[56]|8[56])\\d{8}");
    }

    public static boolean isChinaTelecom(String str) {
        return isRegexMatch(str, "(?!00|015|013)(0\\d{9,11})|(1(33|53|80|89)\\d{8})");
    }

    /** @deprecated */
    @Deprecated
    public static boolean isChinaPAS(String str) {
        return isChinaTelecom(str);
    }

    public static boolean isDate(String str) {
        if (!isEmpty(str) && str.length() <= 10) {
            String[] items = str.split("-");
            if (items.length != 3) {
                return false;
            } else if (isNumber(items[0], 1900, 9999) && isNumber(items[1], 1, 12)) {
                int year = Integer.parseInt(items[0]);
                int month = Integer.parseInt(items[1]);
                return isNumber(items[2], 1, DateUtil.getMaxDayOfMonth(year, month - 1));
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean isDateTime(String str) {
        if (!isEmpty(str) && str.length() <= 20) {
            String[] items = str.split(" ");
            if (items.length != 2) {
                return false;
            } else {
                return isDate(items[0]) && isTime(items[1]);
            }
        } else {
            return false;
        }
    }

    public static boolean isEmail(String str) {
        return isRegexMatch(str, ".+@.+\\.[a-z]+");
    }

    public static boolean isEmpty(Object[] args) {
        return args == null || args.length == 0 || args.length == 1 && args[0] == null;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isIdCardNumber(String str) {
        return isRegexMatch(str, "(\\d{14}|\\d{17})(\\d|x|X)");
    }

    public static boolean isMobile(String str) {
        return isChinaMobile(str) || isChinaUnicom(str) || isChinaPAS(str);
    }

    public static boolean isNumber(String str) {
        if (isEmpty(str)) {
            return false;
        } else {
            for(int i = 0; i < str.length(); ++i) {
                if (str.charAt(i) > '9' || str.charAt(i) < '0') {
                    return false;
                }
            }

            return true;
        }
    }

    public static boolean isNumber(String str, int min, int max) {
        if (!isNumber(str)) {
            return false;
        } else {
            int number = Integer.parseInt(str);
            return number >= min && number <= max;
        }
    }

    public static boolean isNumeric(String str) {
        return isRegexMatch(str, "(\\+|-){0,1}(\\d+)([.]?)(\\d*)");
    }

    public static boolean isNumeric(String str, int fractionNum) {
        if (isEmpty(str)) {
            return false;
        } else {
            String regex = "(\\+|-){0,1}(\\d+)([.]?)(\\d{0," + fractionNum + "})";
            return Pattern.matches(regex, str);
        }
    }

    public static boolean isPhoneNumber(String str) {
        return isRegexMatch(str, "(([\\(（]\\d+[\\)）])?|(\\d+[-－]?)*)\\d+");
    }

    public static boolean isPostcode(String str) {
        if (isEmpty(str)) {
            return false;
        } else {
            return str.length() == 6 && isNumber(str);
        }
    }

    public static boolean isString(String str, int minLength, int maxLength) {
        if (str == null) {
            return false;
        } else if (minLength < 0) {
            return str.length() <= maxLength;
        } else if (maxLength < 0) {
            return str.length() >= minLength;
        } else {
            return str.length() >= minLength && str.length() <= maxLength;
        }
    }

    public static boolean isTime(String str) {
        if (!isEmpty(str) && str.length() <= 8) {
            String[] items = str.split(":");
            if (items.length != 2 && items.length != 3) {
                return false;
            } else {
                for(int i = 0; i < items.length; ++i) {
                    if (items[i].length() != 2 && items[i].length() != 1) {
                        return false;
                    }
                }

                return isNumber(items[0], 0, 23) && isNumber(items[1], 0, 59) && (items.length != 3 || isNumber(items[2], 0, 59));
            }
        } else {
            return false;
        }
    }


    public static boolean isRegexMatch(String str, String regex) {
        return str != null && str.matches(regex);
    }

    // 验证用户名
    public static boolean checkUserName(String userName){
        // 验证是否符合正则表达式
        String reg = "^[0-9a-zA-Z_]{5,16}$";
        if(!isRegexMatch(userName,reg)) {
            return false;
        }

        // 验证第一个字符是否字母开头
        String c = userName.substring(0,1);
        if(!isRegexMatch(c,"^[A-Za-z]{1}$")) {
            return false;
        }
        return true;
    }

    // 验证密码
    public static boolean checkPassword(String password){
        String reg = "^[0-9a-zA-Z`~!@#$%&*(),.<>:;\\{\\}\\[\\]]{6,16}";
        if(!isRegexMatch(password,reg)){
            return false;
        }
        return true;
    }
}
